package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.bean.MyCouponBean;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.viewModel.ICheckCouponListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class CouponsCheckListAdapter extends BaseAdapter
{
    private Context context;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    private  MyCouponBean myCouponBean;
    private List beSelectedData = new ArrayList();
    private ICheckCouponListView iCheckCouponListView;
    public CouponsCheckListAdapter(Context context,MyCouponBean myCouponBean,List beSelectedData,HashMap<Integer, Boolean> isSelected,ICheckCouponListView iCheckCouponListView)
    {
        this.context=context;
        this.myCouponBean=myCouponBean;
        this.beSelectedData=beSelectedData;
        this.iCheckCouponListView=iCheckCouponListView;
        this. isSelected = isSelected;
    }
    public void addData(MyCouponBean bean,List data,HashMap<Integer, Boolean> isSelected){
        myCouponBean.getData().addAll(bean.getData());
        beSelectedData.addAll(data);
        this.isSelected=isSelected;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount()
    {
        return myCouponBean.getData().size();
    }

    @Override
    public Object getItem(int position)
    {
        return myCouponBean.getData().get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        try{
            ViewHolder viewHolder = null;
            if (convertView == null)
            {
                viewHolder = new ViewHolder();
                convertView= LayoutInflater.from(context).inflate(R.layout.item_couponscheck_list,null);
                viewHolder.tvMoney=(TextView)convertView.findViewById(R.id.tv_coupons_money);
                viewHolder.tvTitle=(TextView)convertView.findViewById(R.id.tv_coupons_title);
                viewHolder.tvContent=(TextView)convertView.findViewById(R.id.tv_coupons_content);
                viewHolder.tvTime=(TextView)convertView.findViewById(R.id.tv_coupons_time);
                viewHolder.cb_checkcoupon=(CheckBox)convertView.findViewById(R.id.cb_checkcoupon);

                convertView.setTag(viewHolder);
            } else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.cb_checkcoupon.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    iCheckCouponListView.setMoney(myCouponBean.getData().get(position).getPrice(),myCouponBean.getData().get(position).getId());
                    // 当前点击的CB
                    boolean cu = !isSelected.get(position);
                    // 先将所有的置为FALSE
                    for(Integer p : isSelected.keySet()) {
                        isSelected.put(p, false);
                    }
                    // 再将当前选择CB的实际状态
                    isSelected.put(position, cu);
                    CouponsCheckListAdapter.this.notifyDataSetChanged();
                    beSelectedData.clear();
                    if(cu)
                    {
                        beSelectedData.add(myCouponBean.getData().get(position));
                    }

                }
            });
            // 根据isSelected来设置checkbox的选中状况
            viewHolder.cb_checkcoupon.setChecked(isSelected.get(position));
            MyCouponBean.Data data=myCouponBean.getData().get(position);
            viewHolder.tvMoney.setText(data.getPrice());
            viewHolder.tvTitle.setText(data.getTitle());
            SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
            String nickname=sharedPreferences.getString("nickname","xxx");
//            viewHolder.tvContent.setText("用户"+ nickname+"消费优惠，单次消费仅限一张不找零");
            viewHolder.tvContent.setText(data.getIntro());
            viewHolder.tvTime.setText(TimeUtils.getTime(Long.parseLong(data.getEtime()), new SimpleDateFormat("yyyy年MM月dd日")) + "过期");
        }catch (Exception e){}

        return convertView;
    }


    public static class ViewHolder
    {
        TextView tvMoney;
        TextView tvTitle;
        TextView tvContent;
        TextView tvTime;
        public CheckBox cb_checkcoupon;
    }
}
